// pages/schoolfellow/schoolfellow.js
const app = getApp()
let _this;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list:[],
        page:1,
        load:false,
        size:10,
        flag:0,
        url:'get',
        wheres:`'a_id=' + wx.getStorageSync("area").pk_id`,
        sorts:"",
        fields:'',
        a_id: wx.getStorageSync("area").pk_id,
        wx_id: wx.getStorageSync("user").id,
        Refresh: false,
        isComment: false, //是否可以评论
        compose_id: '', //帖子id
        comment: '', //评论内容
        blur: false, //控制更多面板显示
        moveY: 0, //用于隐藏更多
        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
        wx.getStorageSync('statusBarHeight') +
        wx.getStorageSync('navigationBarHeight') +'px',
        imgurls:[
            {cover:'http://tieba.work211.cn/plugins/mok_bgimg/img/3.jpg'},
            {cover:'http://tieba.work211.cn/plugins/mok_bgimg/img/3.jpg'},
            {cover:'http://tieba.work211.cn/plugins/mok_bgimg/img/3.jpg'},
        ],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      _this = this
      _this.getList(0)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      if(this.data.Refresh){
        // console.log('刷新');
        this.setData({
          Refresh:false
        })
        _this.getList(0)
      }
    },

  // 删除
    del(e){
      let id = e.currentTarget.dataset.id;
      wx.showModal({
        title:'提示',
        content: '删除后该数据将无法恢复，是否继续?',
        confirmText: '删除',
        cancelText: '点错了',
        success(res){
          if(res.confirm){
            wx.showLoading({
              title: '删除中',
              task:true,
              mask:true
            })
            app.com.post('schoolfellow/wxdel',{
              id:id
            },function(res){
              wx.hideLoading()
              if(res.code == 1){
                wx.showToast({
                  title: '删除成功',
                  task:true
                })
              _this.getList(0)
              }else{
                wx.showToast({
                  text: '删除失败',
                  icon: 'none'
                })
              }
            })
          }
        }
      })
    },

    // 展示更多功能
    showMore(){
      _this.setData({
        blur: true
      })
    },

    // 隐藏更多
    hiddenMask(){
      _this.setData({
        blur: false,
      })
    },

    navTo(e) {
      let state = wx.getStorageSync('res').state
      if(state > 0){
        app.com.navTo(e)
      }else{
        wx.showToast({
          title: '您还未认证',
          icon:'error'
        })
        setTimeout(() => {
          wx.navigateTo({
            url: '/pages/educational/edu/edu',
          })
        }, 500);
      }
    },
    // 获取帖子列表
    getList(type){
        if (type == 0) {
          this.data.page = 1
        } else {
          this.data.page += 1
        }
        this.setData({
          load:true
        })
        app.com.post('schoolfellow/'+this.data.url, {
          a_id:wx.getStorageSync("area").pk_id,
          wheres:this.data.wheres,
          sorts:this.data.sorts,
          fields:this.data.fields,
          pageIndex: this.data.page,
          pageSize: this.data.size
        }, function (res) {
          wx.stopPullDownRefresh()
          if (res.code == 1) {
            let re = res.data.list
            for (let i in re) {
              re[i].time = app.com.formatMsgTime(re[i].create_time)
              // 如果存在图片 转换为数组
              if(re[i].picture != ''){
                re[i].picture = re[i].picture.split(',')
              }
            }
            // console.log(re);
            let arr = []
            if (type == 0) {
              arr = re
            } else {
              arr = _this.data.list
              for (let i in re) {
                arr.push(re[i])
              }
            }
            _this.setData({
              list: arr,
              total: res.data.total,
              load:false
            })
          } else {
            // _this.setData({
              
            //   load: false
            // })
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        })

    },

    zan(e){
      let compose_id=e.currentTarget.dataset.id
      console.log();
      app.com.post('schoolfellow/zan',{

      },function(res){
        if(res.code == 1){
          console.log('点赞成功！');
        }else{
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    },

    // 弹出评论框
    isComment(e){
      this.setData({
        isComment:true,
        compose_id: e.currentTarget.dataset.id, //帖子id
      })
    },
    hiddenComment(){
      this.setData({
        isComment: false,
        compose_id: ''
      })
    },
    // 评论
    comment(e){
      let status=e.currentTarget.dataset.status==1?1:0
      if(this.data.comment != ''){
        app.com.post('schoolfellow/comment',{
          compose_id: this.data.compose_id, //帖子id
          comment:this.data.comment,
          status, //是否匿名
          from_wx_id:this.data.wx_id, //评论用户id
        },function(res){
          // console.log(res);
          if(res.code == 1){
            wx.showToast({
              title: '评论成功',
              icon: 'none'
            })
            _this.hiddenComment()
          }else{
            wx.showToast({
              title: res.msg,
              icon: 'none'
            })
          }
        })
        this.setData({
          comment: '',
          isComment:false
        })
      }else{
        wx.showToast({
          title: '评论内容不能为空',
          icon: 'none'
        })
      }
      
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
      _this = this
      _this.getList(0)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.list.length < this.data.total){
            _this.getList(1)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})